package com.iss.service;

import com.iss.dao.CustomerDAO;
import com.iss.dao.OrderDAO;
import com.iss.dao.RoomDAO;
import com.iss.dao.imp.CustomerDAOImp;
import com.iss.dao.imp.OrderDAOImp;
import com.iss.dao.imp.RoomDAOImp;
import com.iss.model.Customer;
import com.iss.model.Order;
import com.iss.model.Room;

import java.util.Calendar;
import java.util.Date;

public class OrderService {
    private CustomerDAO customerDAO = new CustomerDAOImp();
    private RoomDAO roomDAO = new RoomDAOImp();
    private OrderDAO orderDAO = new OrderDAOImp();

    /*前置验证： 电话号码的正确性 房间号码的正确性*/
    public Boolean checkIn(String phoneNo, String roomNO, Integer duration) {
        Boolean flag = false;
        /*
         * dao层
         *   1. 通过手机号码  查询 客户信息
         *   2. 通过 房间编号 查询 房间信息
         * */
        Customer customer = customerDAO.queryCustomerByPhoneNO(phoneNo);
        Room room = null;// 仿照上面，通过房间编号查询房间
        Order order = new Order();
        order.setOderNO(orderDAO.getNewOrderNO());

        Calendar to = Calendar.getInstance();
        to.add(Calendar.DAY_OF_YEAR, duration);

        order.setFrom(new Date());
        order.setTo(to.getTime());
        order.setCustomer(customer);
        flag = orderDAO.insert(order);

        room.setStatus(Room.ROOM_STATUS_IN);// 设置房间状态为入住
        roomDAO.update(room);

        return flag;

    }

    public Order getOrder(String roomNO) {

        return orderDAO.queryByRoomNO(roomNO);
    }

    public boolean checkOut(String roomNO) {
        Order order = orderDAO.queryByRoomNO(roomNO);
        order.setClosed(true);
        boolean flag = orderDAO.update(order);

        Room room = order.getRoom();
        room.setStatus(Room.ROOM_STATUS_SWEEP);// 设置房间状态为打扫
        roomDAO.update(room);
        return flag;

    }
}
